﻿#region Copyright information
// 
// Copyright © 2005-2013 Yongkee Cho. All rights reserved.
// 
// This code is a part of the Biological Object Library and governed under the terms of the
// GNU Lesser General  Public License (LGPL) version 2.1 which accompanies this distribution.
// For more information on the LGPL, please visit http://bol.codeplex.com/license.
// 
// - Filename: GenomicLocus.cs
// - Author: Yongkee Cho
// - Email: yongkeecho@gmail.com
// - Date Created: 2013-01-25 3:25 PM
// - Last Modified: 2013-01-25 3:59 PM
// 
#endregion
using System;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;

namespace BOL.Models.Genome
{
    /// <summary>
    /// Represent genomic coordinate
    /// </summary>
    [DataContract]
    public class GenomicLocus : IGenomicLocus
    {
        #region Public properties

        /// <summary>Gets or sets chromosome.</summary>
        [DataMember]
        public Chromosome Chromosome { get; set; }

        /// <summary>Gets or sets chromosome position.</summary>
        [DataMember]
        public int Position { get; set; }

        #endregion

        #region Constructor

        public GenomicLocus(Chromosome chromosome, int position)
        {
            Chromosome = chromosome;
            Position = position;
        }

        #endregion

        #region Conversion methods

        public static GenomicLocus Parse(string str)
        {
            var match = Regex.Match(str, @"chr(?<chrName>[0-9]+):(?<position>[0-9]+)");
            var group = match.Groups;

            var chromosome = new Chromosome(group["chrName"].Value);
            var position = Int32.Parse(group["position"].Value);

            return new GenomicLocus(chromosome, position);
        }

        public static bool TryParse(string str, out GenomicLocus result)
        {
            result = default(GenomicLocus);

            try
            {
                result = Parse(str);
            }
            catch
            {
                return false;
            }

            return true;
        }

        #endregion

        #region IComparable, IComparable<IGenomicLocus>, IEquatable<IGenomicLocus> implementation

        public int CompareTo(object other)
        {
            if (other == null)
                throw new ArgumentNullException("other");

            if (!(other is IGenomicLocus))
                throw new InvalidCastException("The 'other' argument is not a IGenomicLocus object.");

            return CompareTo((IGenomicLocus)other);
        }

        public int CompareTo(IGenomicLocus other)
        {
            var compare = Chromosome.CompareTo(other.Chromosome);
            return compare == 0 ? Position.CompareTo(other.Position) : compare;
        }

        public bool Equals(IGenomicLocus other)
        {
            return Chromosome.Equals(other.Chromosome) & Position.Equals(other.Position);
        }

        #endregion

        #region Object overriden

        public override int GetHashCode()
        {
            return Chromosome.GetHashCode() ^ Position;
        }

        public override bool Equals(object other)
        {
            if (other == null)
                throw new NullReferenceException();

            if (!(other is GenomicLocus))
                throw new InvalidCastException("The 'other' argument is not a GenomicLocus object.");
            
            return Equals((GenomicLocus)other);
        }

        public override string ToString()
        {
            return String.Format("{0}:{1}", Chromosome, Position);
        }

        #endregion
    }
}
